﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolERP_BLL;
using CoolERP_Entities;
using System.Web.UI.HtmlControls;

namespace CoolERP_Web.User
{
    public partial class WLProperty : BasePage
    {
        private const int ROOT = 0;
        private const int WL = 1;
        private const int WLSX = 2;
        private const int WLSXZ = 3;
        private string vpath = null;
        private int SelDepth
        {
            get { return ViewState["SelDepth"] == null ? 0 : Convert.ToInt32(ViewState["SelDepth"]); }
            set { ViewState["SelDepth"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitRoot();
                BindGridView();
            }
        }

        #region=======GridView处理=======
        private void BindGridView()
        {
            switch (SelDepth)
            {
                case 0:
                    using (CS_WLLXBLL cBLL = new CS_WLLXBLL())
                    {
                        GridView1.DataSource = cBLL.GetAll();
                        GridView1.DataBind();
                    }
                    break;
                case 1:
                    using (CS_WLLXSXBLL cBLL = new CS_WLLXSXBLL())
                    {
                        GridView1.DataSource = cBLL.GetList(t => t.WLLXBH == tvWL.SelectedValue);
                        GridView1.DataBind();
                    }
                    break;
                case 2:
                    using (CS_WLSXZBLL cBLL = new CS_WLSXZBLL())
                    {
                        string[] BHs = tvWL.SelectedNode.ValuePath.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
                        GridView1.DataSource = cBLL.GetList(t => t.WLLXBH == BHs[1] && t.WLSXBH == BHs[2]);
                        GridView1.DataBind();
                    }
                    break;
                case 3:
                    using (CS_WLSXZBLL cBLL = new CS_WLSXZBLL())
                    {
                        string[] BHs = tvWL.SelectedNode.ValuePath.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
                        GridView1.DataSource = cBLL.GetList(t => t.WLLXBH == BHs[1] && t.WLSXBH == BHs[2] && t.BH == BHs[3]);
                        GridView1.DataBind();
                    }
                    break;
                default:
                    break;
            }
        }

        protected void GridView1_PreRender(object sender, EventArgs e)
        {
            if (GridView1.Rows.Count == 0)
                Utility.RenderEmptyGridView(GridView1, "BH,MC");
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                HtmlInputCheckBox cBox = (HtmlInputCheckBox)e.Row.FindControl("chkSel");
                if (cBox != null)
                {
                    switch (SelDepth)
                    {
                        case 0:
                            CS_WLLX wllx = e.Row.DataItem as CS_WLLX;
                            if (wllx != null)
                                cBox.Value = wllx.BH;
                            break;
                        case 1:
                            CS_WLLXSX wllxsx = e.Row.DataItem as CS_WLLXSX;
                            if (wllxsx != null)
                                cBox.Value = string.Format("{0},{1}", wllxsx.WLLXBH, wllxsx.BH);
                            break;
                        case 2:
                        case 3:
                            CS_WLSXZ wlsxz = e.Row.DataItem as CS_WLSXZ;
                            if (wlsxz != null)
                                cBox.Value = string.Format("{0},{1},{2}", wlsxz.WLLXBH, wlsxz.WLSXBH, wlsxz.BH);
                            break;
                        default:
                            break;
                    }
                }
            }
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            GridView1.ShowFooter = false;
            BindGridView();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            BindGridView();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            TextBox txtMC = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtMC");
            HtmlInputCheckBox cBox = (HtmlInputCheckBox)GridView1.Rows[e.RowIndex].FindControl("chkSel");
            string MC = txtMC.Text.ToString().Trim();
            string[] BHs = cBox.Value.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            int result = 0;
            switch (BHs.Length)
            {
                case 1:
                    using (CS_WLLXBLL cBLL = new CS_WLLXBLL())
                    {
                        CS_WLLX item = cBLL.GetEntity(t => t.BH == BHs[0]);
                        item.MC = MC;
                        result = cBLL.SaveEntity(item, t=>t.BH != BHs[0] && t.MC == MC);
                    }
                    break;
                case 2:
                    using (CS_WLLXSXBLL cBLL = new CS_WLLXSXBLL())
                    {
                        CS_WLLXSX item = cBLL.GetEntity(t => t.WLLXBH == BHs[0] && t.BH == BHs[1]);
                        item.MC = MC;
                        result = cBLL.SaveEntity(item, t => t.WLLXBH == BHs[0] && t.BH != BHs[1] && t.MC == MC);
                    }
                    break;
                case 3:
                    using (CS_WLSXZBLL cBLL = new CS_WLSXZBLL())
                    {
                        CS_WLSXZ item = cBLL.GetEntity(t => t.WLLXBH == BHs[0] && t.WLSXBH == BHs[1] && t.BH == BHs[2]);
                        item.MC = MC;
                        result = cBLL.SaveEntity(item, t => t.WLLXBH == BHs[0] && t.WLSXBH == BHs[1] && t.BH != BHs[2] && t.MC == MC);
                    }
                    break;
                default:
                    break;
            }
            if (result == -1)
            {
                ShowMessage("已存在此类型名称");
            }
            else if (result == 1)
            {
                GridView1.EditIndex = -1;
                BindGridView();
                ShowMessage("保存成功");
            }
            else
                ShowMessage("出错了，请重新保存");
        }

        protected void btnDel_Click(object sender, ImageClickEventArgs e)
        {
            List<CS_WLLX> lxItems = new List<CS_WLLX>();
            List<CS_WLLXSX> sxItems = new List<CS_WLLXSX>();
            List<CS_WLSXZ> sxzItems = new List<CS_WLSXZ>();
            bool result = false;
            try
            {
                switch (SelDepth)
                {
                    case 0:
                        using (CS_WLLXBLL cBLL = new CS_WLLXBLL())
                        {
                            foreach (GridViewRow container in GridView1.Rows)
                            {
                                HtmlInputCheckBox cBox = (HtmlInputCheckBox)container.FindControl("chkSel");
                                if (cBox != null && cBox.Checked)
                                    lxItems.Add(cBLL.GetEntity(t => t.BH == cBox.Value));
                            }
                            result = cBLL.Deletes(lxItems);
                        }
                        break;
                    case 1:
                        using (CS_WLLXSXBLL cBLL = new CS_WLLXSXBLL())
                        {
                            foreach (GridViewRow container in GridView1.Rows)
                            {
                                HtmlInputCheckBox cBox = (HtmlInputCheckBox)container.FindControl("chkSel");
                                if (cBox != null && cBox.Checked)
                                {
                                    string[] BHs = cBox.Value.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                                    sxItems.Add(cBLL.GetEntity(t => t.WLLXBH == BHs[0] && t.BH == BHs[1]));
                                }
                            }
                            result = cBLL.Deletes(sxItems);
                        }
                        break;
                    case 2:
                    case 3:
                        using (CS_WLSXZBLL cBLL = new CS_WLSXZBLL())
                        {
                            foreach (GridViewRow container in GridView1.Rows)
                            {
                                HtmlInputCheckBox cBox = (HtmlInputCheckBox)container.FindControl("chkSel");
                                if (cBox != null && cBox.Checked)
                                {
                                    string[] BHs = cBox.Value.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                                    sxzItems.Add(cBLL.GetEntity(t => t.WLLXBH == BHs[0] && t.WLSXBH == BHs[1] && t.BH == BHs[2]));
                                }
                            }
                            result = cBLL.Deletes(sxzItems);
                        }
                        break;
                    default:
                        break;
                }
                if (result)
                    ShowMessage("删除成功");
            }
            catch (Exception ex)
            {
                this.ShowMessage("删除失败\n" + ex.Message);
            }
            finally
            {
                BindGridView();
            }
        }
        #endregion

        #region=======添加类型=======
        protected void btnAdd_Click(object sender, ImageClickEventArgs e)
        {
            GridView1.ShowFooter = true;
            GridView1.EditIndex = -1;
            BindGridView();
        }

        protected void btnSave_Click(object sender, ImageClickEventArgs e)
        {
            TextBox txtBH = GridView1.FooterRow.FindControl("txtBH") as TextBox;
            TextBox txtMC = GridView1.FooterRow.FindControl("txtMC") as TextBox;
            string[] BHs = null;
            switch (SelDepth)
            {
                case 0:
                    using (CS_WLLXBLL cBLL = new CS_WLLXBLL())
                    {
                        CS_WLLX item = new CS_WLLX();
                        item.BH = txtBH.Text.Trim();
                        item.MC = txtMC.Text.Trim();
                        cBLL.Insert(item);
                    }
                    break;
                case 1:
                    BHs = tvWL.SelectedNode.ValuePath.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
                    using (CS_WLLXSXBLL cBLL = new CS_WLLXSXBLL())
                    {
                        CS_WLLXSX item = new CS_WLLXSX();
                        item.BH = txtBH.Text.Trim();
                        item.MC = txtMC.Text.Trim();
                        item.WLLXBH = BHs[1];
                        cBLL.Insert(item);
                    }
                    break;
                case 2:
                case 3:
                    BHs = tvWL.SelectedNode.ValuePath.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
                    using (CS_WLSXZBLL cBLL = new CS_WLSXZBLL())
                    {
                        CS_WLSXZ item = new CS_WLSXZ();
                        item.BH = txtBH.Text.Trim();
                        item.MC = txtMC.Text.Trim();
                        item.WLLXBH = BHs[1];
                        item.WLSXBH = BHs[2];
                        cBLL.Insert(item);
                    }
                    break;
                default:
                    break;
            }
            Referer();
        }

        protected void btnCancel_Click(object sender, ImageClickEventArgs e)
        {
            GridView1.ShowFooter = false;
            BindGridView();
        }
        #endregion

        #region=======TreeView处理=======
        protected void tvWL_SelectedNodeChanged(object sender, EventArgs e)
        {
            SelDepth = tvWL.SelectedNode.Depth;
            GridView1.ShowFooter = false;
            BindGridView();
        }

        protected void tvWL_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
        {
            populateNode(e.Node);
        }

        private void InitRoot()
        {
            TreeNode root = new TreeNode();
            root.Text = "所有物料类型";
            root.Value = "root";
            root.PopulateOnDemand = false;
            root.ImageUrl = "../Images/Icon/home.png";
            root.ExpandAll();

            tvWL.Nodes.Add(root);
            populateNode(root);
        }

        private void populateNode(TreeNode node)
        {
            if (node == null)
            {
                InitRoot();
                return;
            }

            switch (node.Depth)
            {
                case ROOT:
                    loadWLLX(node);
                    break;
                case WL:
                    loadWLSX(node.Value, node);
                    break;
                case WLSX:
                    string[] values = node.ValuePath.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
                    loadWLSXZ(values[WL], node.Value, node);
                    break;
            }
        }

        private void loadWLLX(TreeNode node)
        {
            node.ChildNodes.Clear();
            List<CS_WLLX> wls = new List<CS_WLLX>();
            using (CS_WLLXBLL cBLL = new CS_WLLXBLL())
            {
                wls = cBLL.GetAll();
            }
            foreach (CS_WLLX wl in wls)
            {
                TreeNode child = new TreeNode();
                child.Text = string.Format("{0} {1}", wl.BH.Trim(), wl.MC);
                child.Value = wl.BH;
                child.PopulateOnDemand = true;
                child.ImageUrl = "../Images/Icon/tools.png";
                node.ChildNodes.Add(child);

                if (vpath != null && vpath.StartsWith(child.ValuePath))
                {
                    child.ExpandAll();
                    populateNode(child);
                }
            }
        }

        private void loadWLSX(string wllxbh, TreeNode node)
        {
            node.ChildNodes.Clear();
            List<CS_WLLXSX> wlsxs = new List<CS_WLLXSX>();
            using (CS_WLLXSXBLL cBLL = new CS_WLLXSXBLL())
            {
                wlsxs = cBLL.GetList(t=>t.WLLXBH == wllxbh);
            }

            foreach (CS_WLLXSX wlsx in wlsxs)
            {
                TreeNode child = new TreeNode();
                child.Text = string.Format("{0} {1}", wlsx.BH.Trim(), wlsx.MC);
                child.Value = wlsx.BH;
                child.PopulateOnDemand = true;
                child.ImageUrl = "../Images/Icon/bullets.png";
                node.ChildNodes.Add(child);

                if (vpath != null && vpath.StartsWith(child.ValuePath))
                {
                    child.Expanded = true;
                    populateNode(child);
                }
            }
        }

        private void loadWLSXZ(string wllxbh, string wlsxbh, TreeNode node)
        {
            node.ChildNodes.Clear();
            List<CS_WLSXZ> sxzs =  new List<CS_WLSXZ>();
            using (CS_WLSXZBLL cBLL = new CS_WLSXZBLL())
            {
                sxzs = cBLL.GetList(t => t.WLLXBH == wllxbh && t.WLSXBH == wlsxbh);
            }

            foreach (CS_WLSXZ sxz in sxzs)
            {
                string[] values = node.ValuePath.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);

                TreeNode child = new TreeNode();
                child.Text = string.Format("{0} {1}", sxz.BH.Trim(), sxz.MC); ;
                child.Value = sxz.BH;
                child.PopulateOnDemand = false;
                child.ImageUrl = "../Images/Icon/bullet_black.png";

                node.ChildNodes.Add(child);
            }
        }
        #endregion
    }
}